﻿using System;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media;

namespace GameLogic.Logic
{
    public class StaticCollidableBehavior: GameLoopBehavior<FrameworkElement>, ICollidable
	{
        private double startX, startY;

        #region ICollidable Members
        public Rect Rect
        {
            get
            {
                try
                {
                    UIElement root = this.GameLoop.RootElement as UIElement;
                    Point upperLeft = this.AssociatedObject.TransformToVisual(root).Transform(new Point(0, 0));
                    return new Rect(upperLeft, new Size(this.AssociatedObject.ActualWidth, this.AssociatedObject.ActualHeight));
                }
                catch
                {
                    Point position = new Point(0, 0);
                    return new Rect(position, new Size(this.AssociatedObject.ActualWidth, this.AssociatedObject.ActualHeight));
                }
            }
        }
        #endregion

        public double X
        {
            get { return this.Rect.X; }
        }

        public double Y
        {
            get { return this.Rect.Y; }
        }
       
        protected override void OnGameLoopInitialized()
		{
            this.startX = this.X;
            this.startY = this.Y;

            // Add element to static collision list
            this.GameLoop.RegisterAsStaticCollidable(this.AssociatedObject);
            this.GameLoop.Reset += this.OnReset;
		}

        private void OnReset(object sender, EventArgs e)
        {
            this.UnregisterCollidableIfNecessary();

            this.SetPosition(new Point(this.startX, this.startY));
        }

        private void UnregisterCollidableIfNecessary()
        {
            // check to see if we're still in the tree and remove ourselves if not
            FrameworkElement element = this.AssociatedObject;
            while (element != null)
            {
                if (element.Equals(this.GameLoop.RootElement as FrameworkElement))
                {
                    return;
                }
                element = VisualTreeHelper.GetParent(element) as FrameworkElement;
            }

            // Remove element from static collision list
            this.GameLoop.UnregisterAsStaticCollidable(this.AssociatedObject);
        }

        public void SetPosition(Point p)
        {
            double diffX = p.X - this.X;
            double diffY = p.Y - this.Y;
            MatrixTransform matrixTransform = new MatrixTransform();
            TransformGroup group = new TransformGroup();
            TranslateTransform translateTransform = new TranslateTransform();
            if (this.AssociatedObject.RenderTransform != null)
            {
                group.Children.Add(this.AssociatedObject.RenderTransform);
            }
            translateTransform.X = diffX;
            translateTransform.Y = diffY;
            group.Children.Add(translateTransform);
            matrixTransform.Matrix = group.Value;
            this.AssociatedObject.RenderTransform = matrixTransform;
            this.AssociatedObject.UpdateLayout();
        } 
	}
}
